package com.xx.frame;

import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.project.Project;
import org.jetbrains.idea.maven.project.MavenProject;
import org.jetbrains.idea.maven.project.MavenProjectsManager;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

public class MavenService {

    private static MavenService service = new MavenService();

    public static MavenService getInstance() {
        return service;
    }

    private MavenService() {

    }

    /**
     * 解析 maven 项目
     */
    public List<String> getAllArtifactIds(Project project) {
        try {
            MavenProjectsManager mavenProjectsManager = ServiceManager.getService(project, MavenProjectsManager.class);
            List<MavenProject> mavenProjects = mavenProjectsManager.getProjects();

            List<MavenProject> rootProjects = mavenProjectsManager.getRootProjects();
            List<String> rootArtifactId = rootProjects.stream().map(p -> p.getMavenId().getArtifactId()).collect(Collectors.toList());
//            notifyInfo("root project:" + rootArtifactId);

            return mavenProjects.stream()
                    .map(m -> tree(m, mavenProjects, rootArtifactId, new AtomicInteger(0)))
                    .collect(Collectors.toList());
        } catch (Error e) {
            return Collections.singletonList("error:" + e.getLocalizedMessage());
        }
    }


    /**
     * 获得pom 根 ArtifactId
     */
    public List<String> getRootArtifactId(Project project) {
        MavenProjectsManager mavenProjectsManager = ServiceManager.getService(project, MavenProjectsManager.class);
        List<MavenProject> rootProjects = mavenProjectsManager.getRootProjects();
        return rootProjects.stream().map(p -> p.getMavenId().getArtifactId()).collect(Collectors.toList());
    }

    /**
     * 递归获得 ArtifactId 全路径
     */
    private String tree(MavenProject currentProject, List<MavenProject> mavenProjects, List<String> rootArtifactId, AtomicInteger adder) {

        if (Objects.isNull(currentProject.getMavenId())) {
            return "unknowMavenId";
        }
        if (adder.get() > 7) {
            return "loop7";
        }

        String artifactId = currentProject.getMavenId().getArtifactId();
        StringBuilder info = new StringBuilder();
        info.append("/").append(artifactId);

        for (MavenProject item : mavenProjects) {
            if (Objects.nonNull(currentProject.getParentId())
                    && Objects.equals(currentProject.getParentId().getArtifactId(), item.getMavenId().getArtifactId())) {
                adder.addAndGet(1);

                // 非独苗maven项目 且不是根pom
                if (mavenProjects.size() > 1 && !rootArtifactId.contains(item.getMavenId().getArtifactId())) {
                    info.insert(0, tree(item, mavenProjects, rootArtifactId, adder));
                }
            }
        }
//        if (adder.get() == 0) {
//            info.replace(0, 1, " ");
//        }
        return info.toString();
    }

    /**
     * 获得pom 根 ArtifactId
     */
    public List<MavenProject> getRootMavenProject(Project project) {
        MavenProjectsManager mavenProjectsManager = ServiceManager.getService(project, MavenProjectsManager.class);
        return mavenProjectsManager.getRootProjects();
    }

}
